package com.moneywelcome.atomloanassembly.apiimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moneywelcome.atomloanassembly.model.LoanProduct;
import com.moneywelcome.atomloanassembly.service.LoanProductService;
import com.moneywelcome.atomloanfacade.api.LoanProductApi;
import com.moneywelcome.atomloanfacade.info.LoanProductInfo;
import com.moneywelcome.params.PageParam;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoanProductApiImpl implements LoanProductApi {

    @Autowired
    private LoanProductService loanProductService;
    /**
     * 通过id查询贷款产品
     * @param loanProductId
     * @return
     */
    @Override
    public Result<LoanProductInfo> getLoanProductById(Integer loanProductId) {
        LoanProduct loanProduct = loanProductService.getById(loanProductId);
        return null==loanProduct?Result.fail("没有该产品"):Result.success(loanProduct);
    }

    /**
     * 分页查询寻贷款产品
     * @param form
     * @return
     */
    @Override
    public Result<Page<LoanProductInfo>> getLoanProducts(PageParam form) {
        Page<LoanProduct> productPage = loanProductService.page(new Page<LoanProduct>());
        return Result.success(PageResult.getPageResult(productPage));
    }

    /**
     * 通过产品可贷款最低评分分页查询贷款产品
     * @param baseGrade
     * @param form
     * @return
     */
    @Override
    public Result<Page<LoanProductInfo>> getLoanProductsByBaseGrade(Integer baseGrade, PageParam form) {
        Page<LoanProduct> productPage = loanProductService.page(new Page<LoanProduct>(), new QueryWrapper<LoanProduct>().gt("base_grade", baseGrade));
        return Result.success(PageResult.getPageResult(productPage));
    }
}
